﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using Microsoft.Office.Interop.Excel;

public partial class BSysWebGroup : INTPage
{
    SqlHelp help = new SqlHelp();
    public int rowNo = 0;
    CommonMethod comHelp = new CommonMethod();
    private string errorTimeOutUrl = "../../error.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        if (!IsPostBack)
        { WebGroupBind(Session["UserCode"].ToString()); }
    }

    #region 初始化绑定列表数据 WebsBind(string UserCode)
    protected void WebGroupBind(string UserCode)
    {   
        // 页面权限
        comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "BSysWebGroupM" }, GrvWebGroup, new string[] { "cmdAdd", "cmdEdit" }, new int[] { 4, 5 }, "../../SLogin.aspx");
      
        QueryBind(0);
    }
    #endregion

    #region 换页
    protected void GrvWebGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, "../../error.aspx");
        try
        {            
            GrvWebGroup.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch
        { }
    }
    #endregion
    #region 查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
    }
    //
    protected void QueryBind(int n)
    {
        string StrSql = " select  SWG.WGCode,SWG.WGName,SWG.WGDesc "
            + "from B_SysWebGroup SWG where DeleteFlag='0' ";

        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtWGCode.Text.ToString()))
            { StrSql += string.Format(" and SWG.WGCode like '%{0}%'" , txtWGCode.Text.ToString() ); }
            if (!string.IsNullOrEmpty(txtWGName.Text.ToString()))
            { StrSql += string.Format(" and SWG.WGName like '%{0}%'" , txtWGName.Text.ToString() ); }
            if (!string.IsNullOrEmpty(txtWGDesc.Text.ToString()))
            { StrSql += string.Format(" and SWG.WGDesc like '%{0}%'", txtWGDesc.Text.ToString()); }
        }
        StrSql += " order by SWG.WGCode DESC ";
        DataTable Dt = help.GetDataTable(StrSql);
        GrvWebGroup.DataSource = Dt;
        GrvWebGroup.DataBind();
        
        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            GrvWebGroup.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = GrvWebGroup.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }
    #endregion
    #region 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        //统计列表中与要添加的类型相同的个数
        int ComN = 0;
        //;
        string StrGvNSql = string.Format("select count(*) from B_SysWebGroup where WGCode='{0}'", txtWGCode.Text.ToString());
        ComN = Convert.ToInt16(help.RunSqlReturn(StrGvNSql));
        if (ComN < 1)
        {
            //获取当前用户名称
            string UserName = Session["UserCode"].ToString();
            //当前时间
            string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");

            string StrInsert = string.Format("Insert Into B_SysWebGroup(WGCode,WGName,WGDesc,DeleteFlag,AddAuthor,"
                + "AddDate,UpdateAuthor,UpdateDate) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}') ", txtWGCode.Text.ToString(), txtWGName.Text.ToString(), txtWGDesc.Text.ToString(), "0", UserName, StrDate, UserName, StrDate);
            //添加结果
            int result = help.RunSqlInt(StrInsert);
            if (result > 0)
            {
                CommonMethod.regAlertMessage("操作成功-Success!", this);
                QueryBind(0);
                HidQueryFlag.Value = "0";

                txtWGCode.Focus();
            }
            else
            {
                CommonMethod.regAlertMessage("出现错误-Error!", this);
                txtWGCode.Focus();
            }
        }
        else { CommonMethod.regAlertMessage(txtWGCode.Text.ToString() + "  已经存在！", this); }
    }
    #endregion
    #region 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        string StrUpdate = string.Format(" Update B_SysWebGroup set WGName='{0}',WGDesc='{1}'  where WGCode='{2}' ", txtWGName.Text.ToString(), txtWGDesc.Text.ToString(), txtWGCode.Text.ToString());
        if (help.RunSqlInt(StrUpdate) > 0)
        {
            CommonMethod.regAlertMessage("更新成功！", this);
            QueryBind(0);
            HidQueryFlag.Value = "0";

            cmdAdd.Enabled = true;
            cmdEdit.Enabled = false;
            txtWGCode.Enabled = true;
            txtWGCode.Text = "";
            txtWGDesc.Text = "";
            txtWGName.Text = "";
        }
    }
    #endregion

    #region GridView 行事件
    //Gridview 行事件
    protected void GrvWebGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        rowNo++;
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                if ("zh-cn".Equals(Session["Language"].ToString()))
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");
                }
                else
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('OK to delete?');");
                }
            }
        }
    }
    //行删除
    protected void GrvWebGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, "../../error.aspx");

        //获取并判断此项在页面与页面组中是否存在
        string WGCode = GrvWebGroup.Rows[e.RowIndex].Cells[1].Text.Trim().ToString();
        int ComN = Convert.ToInt16(help.RunSqlReturn(string.Format("select count(*) from B_SysWebAndGroup where WGCode='{0}'", WGCode)));
        if (ComN < 1)
        {
            string StrDelete = string.Format(" Delete from B_SysWebGroup where WGCode='{0}'", WGCode);
            if (help.RunSqlInt(StrDelete) > 0)
            {
                CommonMethod.regAlertMessage("删除成功！", this);
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
            else
            {
                CommonMethod.regAlertMessage("更新失败！", this);
            }
        }
        else
        {
            CommonMethod.regAlertMessage(WGCode + "  此页面组存在于页面与组，现在不可以删除，要删除请先到页面组管理删除此页面！", this);
        }
    }
    //行编辑
    protected void GrvWebGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtWGCode.Text = GrvWebGroup.Rows[GrvWebGroup.SelectedIndex].Cells[1].Text.Trim().ToString();
        txtWGCode.Enabled = false;
        txtWGName.Text = GrvWebGroup.Rows[GrvWebGroup.SelectedIndex].Cells[2].Text.Trim().ToString();
        txtWGDesc.Text = GrvWebGroup.Rows[GrvWebGroup.SelectedIndex].Cells[3].Text.Trim().ToString();

        cmdAdd.Enabled = false;
        cmdEdit.Enabled = true;
    }
    #endregion
    
    //重置
    protected void cmdClear_ServerClick(object sender, EventArgs e)
    {
        cmdAdd.Enabled = true;
        cmdEdit.Enabled = false;
        txtWGCode.Enabled = true;
        txtWGCode.Text = "";
        txtWGDesc.Text = "";
        txtWGName.Text = "";
    }
    //跳页
    protected void GrvWebGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = GrvWebGroup.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                GrvWebGroup.PageIndex = pageindex - 1;
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
}
